package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.LotteryDao;
import com.qdlc.p2p.dal.dto.Lottery;
import com.qdlc.p2p.dal.model.LotteryModel;

@Repository("lotteryDao")
public class LotteryDaoImpl extends BaseDaoImpl<Lottery> implements LotteryDao {

    @Override
    public List<Lottery> getLotteryByCategory(int category) {
        String sql = "from Lottery where status = :status and category = :category";
        Query query = em.createQuery(sql);
        query.setParameter("status", 1);
        query.setParameter("category", category);
        return query.getResultList();
    }


    @Override
    public double getTotalRate(int category) {
        String jpql = "select sum(rate) from " + Global.DB_PREFIX + "lottery where status = :status and category = :category";
        Query query = em.createQuery(jpql);
        query.setParameter("status", LotteryModel.STATUS_ENABLE);
        query.setParameter("category", category);
        double result = 0;
        Object obj = query.getSingleResult();
        if (obj != null) {
            result = Double.parseDouble(obj.toString());
        }
        return result;
    }

    /**
     * 查询所有抽奖配置
     *
     * @return
     */
    @Override
    public PageDataList<Lottery> getAllLottery(LotteryModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        return this.findPageList(param);
    }
}
